package com.company.huisuo.repository

import com.company.huisuo.biz.bean.MerchantAtmosphereBean
import com.company.huisuo.biz.bean.MerchantAtmosphereResponse
import com.company.huisuo.biz.bean.AppSwitchAtmosphereReqVO
import com.company.huisuo.biz.bean.SwitchAtmosphereResponse
import com.company.huisuo.biz.bean.AppStartSessionReqVO
import com.company.huisuo.biz.bean.StartSessionResponse
import com.company.huisuo.biz.bean.AppRoomStyleSelectReqVO
import com.company.huisuo.biz.bean.RoomStyleSelectResponse
import com.company.huisuo.biz.bean.AppEndSessionReqVO
import com.company.huisuo.biz.bean.EndSessionResponse
import com.company.huisuo.network.RetrofitClient
import com.company.huisuo.network.api.MerchantAtmosphereApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * 商家氛围数据仓库
 */
class MerchantAtmosphereRepository {
    
    private val merchantAtmosphereApi: MerchantAtmosphereApi by lazy {
        RetrofitClient.createService()
    }
    
    /**
     * 获得商家氛围列表
     * @param sessionId 场次ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun getMerchantAtmosphereList(
        sessionId: String,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<List<MerchantAtmosphereBean>> = withContext(Dispatchers.IO) {
        try {
            val response = merchantAtmosphereApi.getMerchantAtmosphereList(sessionId, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "获取商家氛围列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 切换氛围
     * @param actualType 实际类型 风格 氛围
     * @param atmosphereId 氛围ID
     * @param sessionId 场次ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun switchAtmosphere(
        actualType: String,
        atmosphereId: Long,
        sessionId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<Boolean> = withContext(Dispatchers.IO) {
        try {
            val request = AppSwitchAtmosphereReqVO(actualType, atmosphereId, sessionId)
            val response = merchantAtmosphereApi.switchAtmosphere(request, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "切换氛围失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 开始场次
     * @param roomCode 房间编码
     * @param merchantCode 商户编码
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun startSession(
        roomCode: String,
        merchantCode: String,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<Long> = withContext(Dispatchers.IO) {
        try {
            val request = AppStartSessionReqVO(roomCode, merchantCode)
            val response = merchantAtmosphereApi.startSession(request, tenantId, authorization)
            if (response.code == 0) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "开始场次失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 房间风格选择
     * @param sessionId 场次ID
     * @param styleId 房间风格ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun roomStyleSelect(
        sessionId: Long,
        styleId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<Boolean> = withContext(Dispatchers.IO) {
        try {
            val request = AppRoomStyleSelectReqVO(sessionId, styleId)
            val response = merchantAtmosphereApi.roomStyleSelect(request, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "房间风格选择失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
    
    /**
     * 结束场次
     * @param sessionId 场次ID
     * @param tenantId 租户编号（可选）
     * @param authorization 认证Token（可选）
     */
    suspend fun endSession(
        sessionId: Long,
        tenantId: Int? = null,
        authorization: String? = null
    ): Result<Boolean> = withContext(Dispatchers.IO) {
        try {
            val request = AppEndSessionReqVO(sessionId)
            val response = merchantAtmosphereApi.endSession(request, tenantId, authorization)
            if (response.code == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.msg ?: "结束场次失败"))
            }
        } catch (e: Exception) {
            Result.failure(Exception("网络请求失败: ${e.message}"))
        }
    }
}